<?
/**
 * <b>Hli_Api Class</b>
 * Published 21/12/2010 03:45:13
 *
 * @author Lars Sorhus (lars@saucecode.com.au)
 * @copyright sauceCode Pty Ltd.
 * @version 2.0.4.342.
 * @internal It's not recommended at all to modify this code.
 */
require_once("Hli_Constants.php");
require_once("Hli_RegisterCredentialsType.php");
require_once("Hli_BookingRequestType.php");
require_once("Hli_ContactType.php");
require_once("Hli_AddressType.php");
require_once("Hli_CollectionType.php");
require_once("Hli_DestinationType.php");
require_once("Hli_ShippingInformationType.php");
require_once("Hli_ErrorType.php");

//Begin Class
class Hli_Api{
	public $nusoapClient;
	public $nusoapProxy;
	private $__credentials;
	private $__environment;
	private $__bookingRequest;
	private $__request;
	private $__response;
	private $__debug_str;

	//Constructor
	public function __construct(){
		$this->__errors=array();
	}

	/**
	 * @method getRegisterCredentials
	 * @return Hli_RegisterCredentialsType
	 */
	public function getRegisterCredentials(){
		return($this->__credentials);
	}
	/**
	 * @method setRegisterCredentials
	 * @param Hli_RegisterCredentialsType - The credentials object.
	 * @return void
	 */
	public function setRegisterCredentials(Hli_RegisterCredentialsType $creds){
		$this->__credentials=$creds;
	}

	/**
	 * @method getEnvironment
	 * @return Hli_Constants::Env
	 */
	public function getEnvironment(){
		return($this->__environment);
	}
	/**
	 * @method setEnvironment
	 * @param Hli_Constants::Env
	 * @return void
	 */
	public function setEnvironment($env){
		$this->__environment=$env;
	}

	/**
	 * @method getBookingRequest
	 * @return Hli_BookingRequestType
	 */
	public function getBookingRequest(){
		return($this->__bookingRequest);
	}
	/**
	 * @method setBookingRequest
	 * @param Hli_BookingRequestType
	 * @return void
	 */
	public function setBookingRequest(Hli_BookingRequestType $bookingRequest){
		$this->__bookingRequest=$bookingRequest;
	}

	/**
	 * @method AddBookingRequest
	 * @param Hli_BookingRequestType - The booking to be placed
	 * @return Hli_BookingResponseType
	 */
	public function AddBookingRequest(Hli_BookingRequestType $bookingRequest=NULL){
		$result=false;
		$gateway=($this->getEnvironment()==Hli_Constants::Env_Production)?Hli_Constants::Gateway_Production:Hli_Constants::Gateway_Sandbox;
		$bookingRequest=($bookingRequest===NULL)?$this->__bookingRequest:$bookingRequest;
		$this->setBookingRequest($bookingRequest);
		$this->nusoapClient=new nusoap_client($gateway.'?wsdl',true);
		$bookingResponse=new Hli_BookingResponseType();
		//Check for an error
		$err=$this->nusoapClient->getError();
		if($err){
			$bookingResponse->addError(new Hli_ErrorType("0xHSD001",$err,Hli_ErrorType::SEVERITY_HIGH));
		}else{
			//Create the proxy
			$this->nusoapProxy=$this->nusoapClient->getProxy();
			$bookingRequest->setRegisterCredentials($this->__credentials->getCredentials($this->getEnvironment()));
			print("<h2>Booking Request Data</h2>");
			print("<pre>");
			print_r($bookingRequest->serializeToArray());
			print("</pre>");
			$BookingResponseRaw=$this->nusoapProxy->AddBookingRequest($bookingRequest->serializeToArray());
			$bookingResponse->setResponseObject($BookingResponseRaw);
			if($this->nusoapProxy->fault){
				$bookingResponse->addError(new Hli_ErrorType("0xHSD002","There was a fault recorded in the booking request. Use the HliBookingResponseType::getNuSoapResponse method to debug this fault.",Hli_ErrorType::SEVERITY_MEDIUM));
			}else{
				// Check for errors
				$err=$this->nusoapProxy->getError();
				if($err){
					$bookingResponse->addError(new Hli_ErrorType("0xHSD003",$err,Hli_ErrorType::SEVERITY_HIGH));
				}
			}

			$bookingResponse->setNuSoapRequest($this->nusoapProxy->request);
			$bookingResponse->setNuSoapResponse($this->nusoapProxy->response);
			$bookingResponse->setNuSoapDebugOutput($this->nusoapProxy->debug_str);
		}
		$result=$bookingResponse;
		return($result);
	}
}
?>
